var loginid,loginuser,loginname,loginkey;
var isjump = 0;//控制翻页
var jumpobj;//跳转对象（page_r 充值  page_c 消费）
var alertmsg = "";
var countdown=60;
layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    laydate = layui.laydate;//日期
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	loginid = $.session.get("userid");
	loginuser = $.session.get("admin_realname");
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	find_store();
});
//查询所有的店铺
function find_store(){
	$.ajax({
		url: "../../../../service",
		cache: false,
		async: false,
		data: {
			proc: "c_admin.store_selall",
			para: "['" + loginname + "','" + loginkey + "',99999999,1]"
		},
		dataType: "json",
		success: function(data) {
			//console.log(JSON.stringify(data));
			var dataHtml='<option value="-1">请选择店铺</option>';
			if(data[0].TAG=='0'){
				top.layer.alert(""+data[0].MSG+"",function(index){
				   top.location.href="login.html";
				   layer.close(index);
			    });
			}else if(data[0].TAG=='2'){
			}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
			}else{
				for(var i=0;i<data.length;i++){
					if(i==data.length-1){
					}else{
						dataHtml += '<option value="'+data[i].ID+'">'+data[i].SHOP_NAME+'</option>';
					}
				}
			}
			$("#shop_id").html(dataHtml);

		}

	});
}
//查询当前选择店铺的人员
/*function find_storepecount(obj){
	var storeid = $(obj).val();
	$.ajax({
		url: "../../../../service",
		cache: false,
		async: false,
		data: {
			proc: "c_admin.h_userall_selbystore",
			para: "['" + loginname + "','" + loginkey + "','"+storeid+"']"
		},
		dataType: "json",
		success: function(data) {
			console.log(JSON.stringify(data));
			if(data[0].TAG=='0'){
				top.layer.alert(""+data[0].MSG+"",function(index){
				   top.location.href="login.html";
				   layer.close(index);
			    });
			}else if(data[0].TAG=='2'){
				 top.layer.msg(""+data[0].MSG+"");
				 $("#shop_id").val("-1");
			}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
				 top.layer.msg("系统错误，请重试！");
				 $("#shop_id").val("-1");
			}
		}

	});
}
*/
//添加管家
function add(){
	var hname = $("#hname").val().replace(/(^\s+)|(\s+$)/g, "");
	if (hname == '') {
		layer.msg("请输入姓名!",{icon:2});
		return;
	}
	if (!checkChinese(hname)) {
		layer.msg("请输入汉字，并且长度为2-4位!",{icon:2});
		return;
	}
	var firstName = hname.substr(0, 1);
	if (!checkbjx(firstName)) {
		layer.msg("请输入百家姓姓名!",{icon:2});
		return;
	}
	var hidcard = $("#hidcard").val();
	if(!IdentityCodeValid(hidcard)){
		layer.msg("身份证号错误!",{icon:2});
		return;
	};
	var htel = $("#htel").val();
	var regPartton=/1[3-8]+\d{9}/;
	if(!htel || htel==null || !regPartton.test(htel)){
		layer.msg("手机号码格式不正确！",{icon:2});
		$("#htel").focus();return;
	}
	var shop_id = $("#shop_id").val();
	if(shop_id=='-1'){
		layer.msg("请选择店铺！",{icon:2});
		return;
	}
	var royalty_ratio = $("#royalty_ratio").val();
	if(isNaN(royalty_ratio) || royalty_ratio==''){
		layer.msg("请输入数字！",{icon:2});
		$("#royalty_ratio").focus();return;
	}
	var h_password = $("#h_password").val().replace(/(^\s+)|(\s+$)/g, "");
    if(h_password.length==0 || h_password==""){
		h_password='88888888';
	}
	var open_xs=$("input[name='mm']:checked").val();
	console.log(loginname + "','" + loginkey + "',0,'"+hname+"','"+hidcard+"','"+htel+"','"+shop_id+"','"+royalty_ratio+"','"+h_password+"','"+open_xs);
    top.layer.confirm('您确定要添加此加盟商小二吗？',{icon: 3,title:'提示信息'},function(index){
		var index = top.layer.msg('添加中，请稍候',{icon: 16,time:false,shade:0.8});
		setTimeout(function(){
			$.ajax({
				url: "../../../../service",
				cache: false,
				data: {
					proc: "c_admin.h_user_ediet",
					//proc: "xmy_zyr.h_user_ediet",
					para: "['" + loginname + "','" + loginkey + "',0,'"+hname+"','"+hidcard+"','"+htel+"','"+shop_id+"','"+royalty_ratio+"','"+h_password+"','"+open_xs+"']"
				},
				dataType: "json",
				success: function(data) {
					console.log("====="+JSON.stringify(data));
					if(data[0].TAG=='0'){
						top.layer.closeAll();
						top.layer.alert(""+data[0].MSG+"",function(index){
						   top.location.href="login.html";
						   layer.close(index);
					    });
					}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
						top.layer.closeAll();
						top.layer.msg("修改失败，请检查您的网络环境!");
						console.log("读取接口失败,参数错误");
					}else if(data[0].TAG == 2){
						top.layer.msg(""+data[0].MSG+"");
					}else{
						//top.layer.closeAll();
						//scan_code(htel,data[0].ORDER_ID);

						top.layer.msg(""+data[0].MSG+"");
						returnlist();
					}
				}
			});
		},1000);
	})
}
//验证百家姓
function checkbjx(name) {
	//js正则表达式  match
	//为什么此时的match方法不对
	if ("赵|钱|孙|李|周|吴|郑|王|冯|陈|楮|卫|蒋|沈|韩|杨|朱|秦|尤|许|何|吕|施|张|孔|曹|严|华|金|魏|陶|姜|戚|谢|邹|喻|柏|水|窦|章|云|苏|潘|葛|奚|范|彭|郎|鲁|韦|昌|马|苗|凤|花|方|俞|任|袁|柳|酆|鲍|史|唐|费|廉|岑|薛|雷|贺|倪|汤|滕|殷|罗|毕|郝|邬|安|常|乐|于|时|傅|皮|卞|齐|康|伍|余|元|卜|顾|孟|平|黄|和|穆|萧|尹|姚|邵|湛|汪|祁|毛|禹|狄|米|贝|明|臧|计|伏|成|戴|谈|宋|茅|庞|熊|纪|舒|屈|项|祝|董|梁|杜|阮|蓝|闽|席|季|麻|强|贾|路|娄|危|江|童|颜|郭|梅|盛|林|刁|锺|徐|丘|骆|高|夏|蔡|田|樊|胡|凌|霍|虞|万|支|柯|昝|管|卢|莫|经|房|裘|缪|干|解|应|宗|丁|宣|贲|邓|郁|单|杭|洪|包|诸|左|石|崔|吉|钮|龚|程|嵇|邢|滑|裴|陆|荣|翁|荀|羊|於|惠|甄|麹|家|封|芮|羿|储|靳|汲|邴|糜|松|井|段|富|巫|乌|焦|巴|弓|牧|隗|山|谷|车|侯|宓|蓬|全|郗|班|仰|秋|仲|伊|宫|宁|仇|栾|暴|甘|斜|厉|戎|祖|武|符|刘|景|詹|束|龙|叶|幸|司|韶|郜|黎|蓟|薄|印|宿|白|怀|蒲|邰|从|鄂|索|咸|籍|赖|卓|蔺|屠|蒙|池|乔|阴|郁|胥|能|苍|双|闻|莘|党|翟|谭|贡|劳|逄|姬|申|扶|堵|冉|宰|郦|雍|郤|璩|桑|桂|濮|牛|寿|通|边|扈|燕|冀|郏|浦|尚|农|温|别|庄|晏|柴|瞿|阎|充|慕|连|茹|习|宦|艾|鱼|容|向|古|易|慎|戈|廖|庾|终|暨|居|衡|步|都|耿|满|弘|匡|国|文|寇|广|禄|阙|东|欧|殳|沃|利|蔚|越|夔|隆|师|巩|厍|聂|晁|勾|敖|融|冷|訾|辛|阚|那|简|饶|空|曾|毋|沙|乜|养|鞠|须|丰|巢|关|蒯|相|查|后|荆|红|游|竺|权|逑|盖|益|桓|公|万俟|司马|上官|欧阳|夏侯|诸葛|闻人|东方|赫连|皇甫|尉迟|公羊|澹台|公冶|宗政|濮阳|淳于|单于|太叔|申屠|公孙|仲孙|轩辕|令狐|锺离|宇文|长孙|慕容|鲜于|闾丘|司徒|司空|丌官|司寇|仉|督|子车|颛孙|端木|巫马|公西|漆雕|乐正|壤驷|公良|拓拔|夹谷|宰父|谷梁|晋|楚|阎|法|汝|鄢|涂|钦|段干|百里|东郭|南门|呼延|归|海|羊舌|微生|岳|帅|缑|亢|况|后|有|琴|梁丘|左丘|东门|西门|商|牟|佘|佴|伯|赏|南宫|墨|哈|谯|笪|年|爱|阳|佟|第五|言|福".search(name) != -1
	) {
		return true;
	}
	else {
		return false;
	}
}

//校验只能输入汉字，并且长度为2-4
function checkChinese(name) {

	reg = /^[\u4E00-\u9FA5]{2,4}$/;

	if (reg.test(name)) {

		return true;
	} else {
		return false;

	}

}
//验证身份证
function IdentityCodeValid(code) {
	var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
	var tip = "";
	var pass= true;

	if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
		tip = "身份证号格式错误";
		pass = false;
	}

   else if(!city[code.substr(0,2)]){
		tip = "地址编码错误";
		pass = false;
	}
	else{
		//18位身份证需要验证最后一位校验位
		if(code.length == 18){
			code = code.split('');
			//∑(ai×Wi)(mod 11)
			//加权因子
			var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
			//校验位
			var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
			var sum = 0;
			var ai = 0;
			var wi = 0;
			for (var i = 0; i < 17; i++)
			{
				ai = code[i];
				wi = factor[i];
				sum += ai * wi;
			}
			var last = parity[sum % 11];
			if(parity[sum % 11] != code[17]){
				tip = "校验位错误";
				pass =false;
			}
		}
	}
   return pass;
}
function scan_code(useretel,order_id){//扫码支付
console.log(order_id+"===order_id");
	$.ajax({
		url : "/guanjias/Scancode_Pay",
		cache : false,
		data : {
			"money":0.01,
			"state":1,//添加管家
			"h_info":useretel,
			"order_id":order_id,
		},
		dataType : "json",
		success : function(data) {
			console.log(JSON.stringify(data));
			$(".scan_info").attr("src",data[0].URL);
			$(".popup").show();
			//用户是否支付
			countdown=60;
			setInterval('scancode_countdown(\''+order_id+'\')',1000);
		}
	});
}


function scancode_countdown(order_id) {
	if (countdown == 1) {
		countdown=60;
		popup_hide();
		return false;
	} else {
		$("#times").html(countdown+"s");
		countdown--;
		user_ispay(order_id);
	}

}
function user_ispay(order_id){//查询用户是否支付
	$.ajax({
		url: "../../../../service",
		cache: false,
		data: {
			proc: "xmy_zyr.user_ispay",
			para:"['" + order_id + "']"
		},
		dataType: "json",
		success: function(data){
			console.log(JSON.stringify(data));
			if(data[0].A_COUNT=='0'){
				if(countdown==1){
					popup_hide();
				}
			}else{
				top.layer.msg('创建管家帐号成功!');
				window.location.href="jointrader_mentg.html";
			}
		}
	});
}
function popup_hide(){
	$(".popup").hide();
	/*top.layer.alert("创建管家帐号失败!",function(index){
		countdown=60;
		layer.close(index);

	});*/
	top.layer.msg("创建管家帐号失败!");
	returnlist();
		//window.location.href="jointrader_mentg.html";
}
//返回
function returnlist(){
	//top.layer.closeAll();
	window.location.href="jointrader_mentg.html";
}
